﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;
using BONSAI.BLL;
using BONSAI.Common;
using System.Linq;

namespace BONSAI.UI.Pages
{
    public partial class search : System.Web.UI.Page
    {
        public string TypeId { get; set; }
        public string Name { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            TypeId = HttpContext.Current.Items[BONSAIConstants.TYPEID] + "";
            Name = HttpContext.Current.Items[BONSAIConstants.NAME] + "";

            BindProduct(TypeId, Name);
        }
        private void BindProduct(string typeId,string name)
        {
            var lst = (new ProductsBo()).GetAll();
            
            if(!string.IsNullOrEmpty(typeId))
            {
                if (typeId.Equals(BONSAIConstants.ISNEW))
                    lst = lst.Where(x => x.Isnew).ToList();
                else
                {
                    lst = string.IsNullOrEmpty(name)
                              ? lst.Where(x => x.TypeProductsID == typeId).ToList()
                              : lst.Where(x => x.TypeProductsID == typeId && x.NameFull.Contains(name)).ToList();
                }
            }
            else
            {
                lst = lst.Where(x => x.NameFull.Contains(name)).ToList();
            }
            foreach (var item in lst)
            {
                if (string.IsNullOrEmpty(item.thumbnail))
                {
                    item.thumbnail = "~/Images/no-image.jpg";
                }
            }
            lvwNews.DataSource = lst;
            lvwNews.DataBind();

        }
        protected int CalNumber(int s,int page)
        {
            int n = s / page;

            if (s % page > 0)
                n += 1;

            return n;
        }

        protected void lvwNews_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            BindProduct(TypeId, Name);
        }

        protected void lvwNews_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            var task = e.CommandName;
            var productId = e.CommandArgument.ToString();

            if (string.IsNullOrEmpty(productId))
            {
                return;
            }
            switch (task)
            {
                case "addtocart":
                    var lst = (new ProductsBo()).GetById(productId);
                    if (lst == null) return;
                    var cart = new ShoppingCart();
                    if (Session[BONSAIConstants.SHOPPING_CART] != null)
                    {
                        cart.gCart = (ArrayList)Session[BONSAIConstants.SHOPPING_CART];
                    }

                    cart.addCart(lst.ProductsCode, lst.NameFull, 1,
                                         Math.Abs(lst.Pricenew - 0) < 1 ? lst.PriceOld : lst.Pricenew, lst.thumbnail);
                    Session[BONSAIConstants.SHOPPING_CART] = cart.gCart;
                    Response.Redirect("~/home-cart/");
                    break;

            }
        }
    }
}